using UnityEngine;
using System.Collections;

public class EventRenameButton : MonoBehaviour {

	GUIButton button;
	public bool isEditing;
	public int textFieldWidth;
	public int textFieldHeight;
	public string charName = "";
	void Start ()
	{
		isEditing = false;
		button = gameObject.GetComponent<GUIButton>();
		button.OnClickCallback += OnClick;
	}
	
	void OnClick(Vector3 pos){
		isEditing = !isEditing;
	//	if (isEditing == false)
	//		CharacterDataManager.GetInstance().GetCurrentCharData().Load();
	}
	void OnGUI(){
		if (isEditing){
			//FRUTA que pone el focus en el proximo GUI supongo?
			GUI.SetNextControlName("myTextField");
			
			charName = GUI.TextField(new Rect(
				Screen.width/2 - textFieldWidth/2, 
				Screen.height/2 - textFieldHeight/2,
				textFieldWidth, 
				textFieldHeight), 
				charName, 14);
			//FRUTA que pone el focus en el proximo GUI supongo?
			GUI.FocusControl("myTextField");
			
			Event ev = Event.current;
   		    if (ev.keyCode == KeyCode.Return){ 
				isEditing = false;
				if (charName != ""){
					CharacterDataManager.GetInstance().GetCurrentCharData()
					.SetName("                           ");
					CharacterDataManager.GetInstance().GetCurrentCharData()
					.SetName(charName);
					charName = "";
				}
			}
			//Label de mierda para simular el tooltip
			if (charName == "" || charName == null){ 
				GUI.Label(new Rect(
					Screen.width/2 - textFieldWidth/2, 
					Screen.height/2 - textFieldHeight/2,
					textFieldWidth, 
					textFieldHeight)
					, " Insert Name");
			}
		}
	}	
}
